package org.example.repository;

import org.example.model.Device;
import org.example.model.Login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;

@Repository
public class LoginDao {
    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public LoginDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

/*    public Login findByLogin(String username, String password, String role) {
        String sql = "SELECT * FROM employee WHERE username = ? AND password = ? AND role = ?";
        try {
            return jdbcTemplate.queryForObject(sql, new Object[]{username, password, role}, new LoginRowMapper());
        } catch (Exception e) {
            return null;
        }
    }*/

    public Login findByLogin(String loginCredential, String password, String role) {
        String sql = "SELECT e.* FROM employee e " +
                "LEFT JOIN t_person p ON e.name = p.name " +
                "WHERE (e.username = ? OR p.phone = ?) " +
                "AND e.password = ? AND e.role = ?";
        System.out.println(sql);
        System.out.println(loginCredential);
        System.out.println(password);
        System.out.println(role);
        try {
            return jdbcTemplate.queryForObject(
                    sql,
                    new Object[]{loginCredential, loginCredential, password, role},
                    new LoginRowMapper()
            );
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }


    public String getNameById(int id) {
        String sql = "SELECT name FROM employee WHERE id = ?";
        return jdbcTemplate.queryForObject(
                sql,
                (rs, rowNum) -> rs.getString("name"),
                id
        );
    }

    /**
     * 注册用户
     * @param login 注册
     * @return 执行结果影响的记录条数
     */
    public int addUser(Login login) {
        String sql = "INSERT INTO employee (username, name, password, role) VALUES (?,?,?,?)";
        return jdbcTemplate.update(sql,
                login.getUsername(),
                login.getName(),
                login.getPassword(),
                login.getRole()
        );
    }


    /**
     * 修改用户密码
     * @param login 登录对象
     * @return 执行结果影响的记录条数
     */
    public int updatePassword(Login login) {
        String sql = "UPDATE employee SET password = ? WHERE id = ?";
        return jdbcTemplate.update(sql,
                login.getPassword(),
                login.getId()
        );
    }

    /**
     * 注销用户
     * @param id 用户编号
     * @return 执行结果影响的记录条数
     */
    public int deleteUser(int id) {
        String sql = "DELETE FROM employee  " +
                "WHERE id = ?";
        return jdbcTemplate.update(sql,
                id
        );
    }

    static class LoginRowMapper implements RowMapper<Login> {
        @Override
        public Login mapRow(ResultSet rs, int rowNum) throws SQLException {
            Login login = new Login();
            login.setId(rs.getInt("id"));
            login.setUsername(rs.getString("username"));
            login.setName(rs.getString("name"));
            login.setPassword(rs.getString("password"));
            login.setRole(rs.getString("role"));
            return login;
        }
    }
}


